/**
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 2010  Sean Beecroft

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

 * @version 1.10 2010-01-01
 * @author Sean Beecroft
 */
package yawiki.client.application;

import javax.swing.plaf.metal.*;
import javax.swing.*;
import yawiki.client.gui.YEditorMainFrame;
import yawiki.client.gui.YMenu;
import yawiki.client.gui.YTextIcons;
import yawiki.client.gui.YToolBar;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
 * @version 1.0 01/12/99
 */
public class YClient extends JFrame implements WindowListener {

    JTree tree = null;
    JDesktopPane desktop = new JDesktopPane();

    public void windowActivated(WindowEvent e) {
    }

    public void windowClosed(WindowEvent e) {
    }

    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    public void windowDeactivated(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }
    

    public void windowIconified(WindowEvent e) {
    }

    public void windowOpened(WindowEvent e) {
    }

    public YClient(String title) {
        super("copyright (c) yawiki.org.");
        addWindowListener(this);
        String mount = ".";
        ImageIcon icon = new ImageIcon("media/favicon.gif");
        this.setIconImage(icon.getImage());
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(null);
        JLabel label = new JLabel("Load Page:");

        final JTextField jTextField = new JTextField("<Page>");

        jTextField.addFocusListener(new FocusListener() {

            public void focusGained(FocusEvent arg0) {
                // TODO Auto-generated method stub
                if (jTextField.getText().equals("<Page>")) {
                    jTextField.setText("");
                }
            }

            public void focusLost(FocusEvent arg0) {
                // TODO Auto-generated method stub
                if (jTextField.getText().equals("")) {
                    jTextField.setText("<Page>");
                }
            }
        });

        int szh = 20;
        Dimension d = new Dimension(100, szh);

        label.setSize(d);
        label.setMinimumSize(d);
        label.setMaximumSize(d);
        label.setLocation(0, 0);

        jTextField.setSize(d);
        jTextField.setMinimumSize(d);
        jTextField.setMaximumSize(d);
        jTextField.setLocation(0, szh);

        controlPanel.add(label);
        controlPanel.add(jTextField);

        JButton jLoadButton = new JButton("Load");
        jLoadButton.setSize(d);
        jLoadButton.setMinimumSize(d);
        jLoadButton.setMaximumSize(d);
        jLoadButton.setLocation(0, szh + szh);

        jLoadButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                try {
                    String validatedText = jTextField.getText();
                    YEditorMainFrame editor = new YEditorMainFrame(validatedText);

                    desktop.add(editor);
                    editor.toFront();

                } catch (Exception ex) {
                    System.out.println(ex);
                }
            }
        });

        controlPanel.add(jLoadButton);

        desktop.setLayout(null);
        JScrollPane fileTreeSp = new JScrollPane(controlPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, fileTreeSp, desktop);
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation(120);

        // Provide minimum sizes for the two components in the split pane
        Dimension minimumSize = new Dimension(100, 500);
        fileTreeSp.setMinimumSize(minimumSize);
        // editorSp.setMinimumSize(minimumSize);

        YEditorMainFrame editor = new YEditorMainFrame(title);
        desktop.add(editor);
        try {
            editor.setSelected(true);
        } catch (Exception ex) {
        }

        JPanel controlBar = new JPanel();
        controlBar.setLayout(new BorderLayout());
        YMenu menu = new YMenu();
        JPanel jMenuPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));

        jMenuPanel.add(menu);
        controlBar.add(jMenuPanel, BorderLayout.NORTH);
        YToolBar toolbar = new YToolBar();
        controlBar.add(toolbar, BorderLayout.CENTER);

        getContentPane().add(controlBar, BorderLayout.NORTH);
        getContentPane().add(splitPane, BorderLayout.CENTER);

    }

    private Hashtable makeIcons() {
        Hashtable icons = new Hashtable();

        icons.put("floppyDrive", MetalIconFactory.getTreeFloppyDriveIcon());
        icons.put("hardDrive", MetalIconFactory.getTreeHardDriveIcon());
        icons.put("computer", MetalIconFactory.getTreeComputerIcon());
        icons.put("c", YTextIcons.getIcon("c"));
        icons.put("cpp", YTextIcons.getIcon("cpp"));
        icons.put("java", YTextIcons.getIcon("java"));
        icons.put("html", YTextIcons.getIcon("html"));
        icons.put("xml", YTextIcons.getIcon("xml"));

        return icons;
    }

    public static void main(String args[]) {
        String vers = System.getProperty("java.version");
        System.out.println("YClient running:" + vers);
        String title = "";
        if (args.length == 0) {
            title = "Null";
        } else {
            title = args[0];
            title = title.replaceAll("&apos;", "'");
            title = title.replaceAll("_", " ");
        }
        if (vers.compareTo("1.1.2") < 0) {
            System.out.println("!!!WARNING: Swing must be run with a 1.1.2 or higher version VM!!!");
        }

        JFrame.setDefaultLookAndFeelDecorated(true);
        YClient frame = new YClient(title);
        int w = 500;
        int h = 500;

        Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();

        frame.setLocation(0, 0);
        frame.setSize(screenDim.width, screenDim.height);
        frame.setVisible(true);

        boolean installHelperApplication = false;
        String os = System.getProperty("os.name");
        System.out.println(os);
        if (os.indexOf("Windows") != -1) {
            // installHelperApplication == false for now until the functionality is finished.
            if (installHelperApplication) {
                //WorkerThread thread = new WorkerThread ();
                //thread.start();
            }
        }
    }
}


